using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace VSSDKAssist.Platform.ToolWindows.UI
{
	public class ExtendedTreeView : TreeView
	{
		public const int NoImage = -1;
		private const int Space = 3;

		public ExtendedTreeView()
			: base()
		{
			base.LineColor = SystemColors.GrayText;
			base.DrawMode = TreeViewDrawMode.OwnerDrawAll;
		}

		protected override void OnDrawNode(DrawTreeNodeEventArgs e)
		{
			e.DrawDefault = true;

			base.OnDrawNode(e);

			if(base.ShowLines && base.ImageList != null && e.Node.ImageIndex == NoImage)
			{
				int imgW = base.ImageList.ImageSize.Width;
				int imgH = base.ImageList.ImageSize.Height;

				int xPos = e.Node.Bounds.Left - Space - imgW / 2;
				int yPos = (e.Node.Bounds.Top + e.Node.Bounds.Bottom) / 2;

				Rectangle imgRect = new Rectangle(xPos, yPos, 0, 0);
				imgRect.Inflate(imgW / 2, imgH / 2);

				using(Pen p = new Pen(base.LineColor, 1))
				{
					p.DashStyle = DashStyle.Dot;
					p.DashOffset = base.Indent % 2;

					int yHor = yPos + ((base.ItemHeight - imgRect.Height) / 2) % 2;

					e.Graphics.DrawLine(p,
						(base.ShowRootLines || e.Node.Level > 0) ? imgRect.Left : xPos - (int)p.DashOffset,
						yHor, imgRect.Right, yHor);


					if(!base.CheckBoxes && e.Node.IsExpanded)
					{
						int yVer = yHor + (int)p.DashOffset;
						e.Graphics.DrawLine(p, xPos, yVer, xPos, e.Node.Bounds.Bottom);
					}
				}
			}
		}

		protected override void OnAfterCollapse(TreeViewEventArgs e)
		{
			base.OnAfterCollapse(e);

			if(!base.CheckBoxes && base.ImageList != null && e.Node.ImageIndex == NoImage)
			{
				base.Invalidate(e.Node.Bounds);
			}
		}
	}
}